---@class UIRewardPreview : UIBase
UIRewardPreview = UIBase.New(PanelResNames.UIRewardPreview)
local t = UIRewardPreview

function t.Init(rewards,subStr,closeCallback)
    t.rewards = rewards
    t.subStr = subStr 
    UIBase.Init(t, closeCallback)
end

function t:OnCreate()
    self.btnCloseReward = self:GetChild("btnCloseReward")
    self.itemGrid = self:GetList("itemGrid")
    self.ctrlShowText = self:GetController("showtext")
    self.showText = self:GetChild("ChapterRewardText")
    self:SetDynamicList(self.itemGrid,self.ItemRender, nil, true)
    self:RegisterOnClick(self.btnCloseReward,self.ClickClose)
    t:InitData()
end

function t:InitData() 
    self.itemGrid.numItems = #t.rewards
    --sub str
    if string.IsNilOrEmpty(t.subStr) then
        self.ctrlShowText.selectedIndex = 0
    elseif t.subStr == getLanguage("ChapterRewardText") then
        self.ctrlShowText.selectedIndex = 1
        self.showText.text = t.subStr
    elseif t.subStr == "通关当前关卡可获得" then
        self.ctrlShowText.selectedIndex = 2
        -- self.showText.text = t.subStr
    else
        self.ctrlShowText.selectedIndex = 0
    end

end
function t:OnDisable()
end
function t:OnDestroy()
end
function t:ItemRender(index,go) 
    local data = t.rewards[index]
    if data ~= nil then
        if data.isHero ~= nil then
            t:initHeroData(go, data)
        else
            local item = logicMgr.CtrlManager.GetCtrl(PanelNames.Item)
            item.initData(go,data) 
        end
        
    end
end 

function t:initHeroData(obj, data)
    local img_Quality = obj:GetChild("quality")
    local img_Frame = obj:GetChild("frame")
    local img_Icon = obj:GetChild("icon")

    if data ~= nil then  
        local hero_cfg = logicMgr.HeroManager.GetHeroConfigbyModelId(data.itemModelId) 
        if hero_cfg ~= nil then
            img_Icon.url = EnumConst.AtlasPath.ICONAtlas .. hero_cfg.f_HeroIcon 
            local quality_index = hero_cfg.f_HeroInitialStar  / 10
            local quality_atlas = Config.IconFrame[quality_index]
            img_Quality.url = "ui://ICONAtlas/" .. quality_atlas
            img_Frame.url = "ui://ICONAtlas/" .. quality_atlas
        end 

        obj.onClick:Clear()
        -- obj.onClick:Add(function()
        -- end)
    end
end

function t:ClickClose()
    self:OnBack()
end

return t